# File：反射购物商城.py
# Author：信思智学
# Date ：2025/5/22 14:10
# Info： 管理员   商家   买家
class Manager:
    # <a href = 'systemManager?id=2' > 系统管理 < / a >
    role_menu = [
        ("【系统管理】","systemManager"),
        ("【账号管理】","numberManager")
    ]

    def __init__(self,name):
        self.name = name

    def systemManager(self):
        print(self.name + "执行系统管理功能...")

    def numberManager(self):
        print(self.name + "执行账号管理功能...")

class Merchant:
    # <a href = 'systemManager?id=2' > 系统管理 < / a >
    role_menu = [
        ("【商品上架】", "goodsAdd"),
        ("【商品下架】", "goodsDelete"),
        ("【商品发货】", "goodsSend")
    ]

    def __init__(self, name):
        self.name = name

    def goodsAdd(self):
        print(self.name + "执行商品上架管理功能...")

    def goodsDelete(self):
        print(self.name + "执行商品下架管理功能...")

    def goodsSend(self):
        print(self.name + "执行商品发货管理功能...")

class Buyer:
    # <a href = 'systemManager?id=2' > 系统管理 < / a >
    role_menu = [
        ("【商品详情】", "goodsInfo"),
        ("【商品下单】", "goodsOrder"),
        ("【商品收货】", "goodsRecv"),
        ("【密码修改】", "updatePass")
    ]

    def __init__(self, name):
        self.name = name

    def goodsInfo(self):
        print(self.name + "执行商品详细信息功能...")

    def goodsOrder(self):
        print(self.name + "执行商品下单功能...")

    def goodsRecv(self):
        print(self.name + "执行商品收货管理功能...")

    def updatePass(self):
        print(self.name + "执行修改密码功能...")




def login():
    username = input("请输入用户名....")
    password = input("请输入密码....")

    with open("userrole") as file:
        for line in file:
            name,password1,role = line.strip().split(",")
            if username == name and password == password1:
                print("登录成功")
                return username,role

import sys
def main():
    user,role = login()
    # print(role,type(role))

    # 获得当前操作的模块
    fileObj = sys.modules["__main__"]
    # 获得模块中的类信息
    cls = getattr(fileObj,role)

    # 由类创建对象
    obj = cls(user)

    # 获得类中的菜单列表
    role_menu = cls.role_menu
    #print(cls.role_menu)

    while 1:
        for i, menu in enumerate(role_menu, 1):
            print(i, menu[0])

        # 接收用户输入的菜单编号
        num = int(input("请输入要操作的菜单编号...."))

        # 获得对应的名称
        name = role_menu[num - 1][1]

        # 调用反射，执行对应的方法
        # print(name)
        getattr(obj,name)()

main()



